package com.easyCode.sqlMapper.mapInfoAdaptor;

import com.easyCode.sqlBuilder.SqlInfo;
import com.easyCode.sqlBuilder.sqlInfo.BaseTableSqlInfo;
import com.easyCode.sqlMapper.mapInfo.TableMapInfo;
import com.easyCode.util.StringUtil;

import java.util.stream.Collectors;


/**
 * @author 大忽悠
 * @create 2022/12/2 14:37
 */
public class TableMapInfoAdaptor implements MapInfoAdaptor<TableMapInfo> {
    private final FiledMapInfoAdaptor filedMapInfoAdaptor=new FiledMapInfoAdaptor();
    /**
     * @param source sqlInfo子类
     * @return 当前适配器是否支持适配当前sqlInfo
     */
    @Override
    public boolean support(SqlInfo source) {
        return source instanceof BaseTableSqlInfo;
    }

    /**
     * @param source sqlInfo子类
     * @return mapInfo子类
     */
    @Override
    public TableMapInfo adapter(SqlInfo source) {
        BaseTableSqlInfo tableSqlInfo = (BaseTableSqlInfo) source;
        return TableMapInfo.builder().tableName(StringUtil.underlineToCamel(tableSqlInfo.getTableName())).comment(tableSqlInfo.getComment())
                .filedMapInfos(tableSqlInfo.getBaseFiledSqlInfoList().stream().map(filedMapInfoAdaptor::adapter).collect(Collectors.toList()))
                .primaryKeyName(StringUtil.underlineToCamel(tableSqlInfo.getPrimaryKeyName())).build();
    }
}
